use zbus::{Connection, dbus_proxy};

#[dbus_proxy(
    interface = "org.freedesktop.PolicyKit1.Authority",
    default_service = "org.freedesktop.PolicyKit1",
    default_path = "/org/freedesktop/PolicyKit1/Authority"
)]
trait PolkitAuthority {
    async fn check_authorization(
        &self,
        subject: (String, std::collections::HashMap<&str, zbus::zvariant::Value<'_>>),
        action_id: &str,
        details: std::collections::HashMap<&str, &str>,
        flags: u32,
        cancellation_id: &str,
    ) -> zbus::Result<(bool, bool, zbus::zvariant::OwnedValue)>;
}

pub async fn check_authorization(action: &str) -> Result<bool, String> {
    let conn = Connection::system().await.map_err(|e| e.to_string())?;
    let proxy = PolkitAuthorityProxy::new(&conn).await.map_err(|e| e.to_string())?;
    
    let mut subject = std::collections::HashMap::new();
    subject.insert("pid", zbus::zvariant::Value::U32(std::process::id()));
    
    let result = proxy.check_authorization(
        ("unix-process".to_string(), subject),
        action,
        std::collections::HashMap::new(),
        1,
        ""
    ).await.map_err(|e| e.to_string())?;
    
    Ok(result.0)
}